package com.wy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.wy.bean.Tarea;
import com.wy.bean.Tscenic;
import com.wy.bean.Tscenicdetail;
import com.wy.bean.Tuser;
import com.wy.dao.TscenicDao;
import com.wy.service.TscenicService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class TscenicController {
    @Resource
    private TscenicService tscenicService;
    @RequestMapping("list")
    public String list(Tscenic tscenic , Model model,
                       @RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "2") int pageSize

                       ){
        PageHelper.startPage(pageNum,pageSize);
        List<Tscenic> list = tscenicService.list(tscenic);
        PageInfo<Tscenic> PageInfo = new PageInfo<>(list);
        model.addAttribute("list",list);
        model.addAttribute("PageInfo",PageInfo);
        model.addAttribute("tscenic",tscenic);
        return "list";
    }
    @RequestMapping("getTscenic")
    public String getTscenic(String id , Model model){
        Tscenic tscenic = tscenicService.getTscenic(id);
        model.addAttribute("tscenic",tscenic);
        return "getTscenic";
    }
    @RequestMapping("tologin")
    public String tologin(){
        return "tologin";
    }
    @RequestMapping("login")
    @ResponseBody
    public boolean login(Tuser tuser , HttpSession httpSession){
        List<Tuser> tuser1 = tscenicService.getTuser(tuser);
        if(tuser1.size()>0){
            httpSession.setAttribute("tuser",tuser1.get(0));
            return true;
        }else {
            return false;
        }
    }
    @RequestMapping("getTarea")
    @ResponseBody
    public List<Tarea> getTarea(String pid){
        return tscenicService.getTarea(pid);
    }
    @RequestMapping("getTscenicdetail")
    @ResponseBody
    public List<Tscenicdetail> getTscenicdetail(String id){
        return tscenicService.getTscenicdetail(id);
    }
    @RequestMapping("toadd")
    public String toadd(Tscenic tscenic,String id,Model model){
        if(StringUtil.isNotEmpty(id)){
            Tscenic tscenics = tscenicService.getTscenic(id);
            model.addAttribute("tscenics",tscenics);
        }
        return "add";
    }
    @RequestMapping("save")
    public String save(Tscenic tscenic){
        String id = tscenic.getId();
        if(StringUtil.isEmpty(id)){
            tscenicService.insert(tscenic);
        }else {
            tscenicService.update(tscenic);
        }

        return "redirect:list.do";
    }

}
